
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.util.Timer;
import java.util.TimerTask;
import javax.swing.JFrame;
import javax.swing.JPanel;

public class Test1 extends JPanel {
    
    public String keyPressed;
	
	public Test1() {
		final KeyListener listener = new MyKeyListener();
		addKeyListener(listener);
		setFocusable(true);
                
                 TimerTask timerTask = new TimerTask() {
                     
                    public void run() {
                        System.out.println(keyPressed);
                    }
                };

                // se crea un objeto del tipo timer
                Timer timer = new Timer();

                // se indica la tarea a ejecutar, 
                //el retardo y cada cuando se tiene que repetir
                timer.scheduleAtFixedRate(timerTask, 0, 100);
	}
	
	public static void main(String[] args) {
		JFrame frame = new JFrame("Mini Tennis");
		Test1 keyboardExample = new Test1();
		frame.add(keyboardExample);
		frame.setSize(200, 200);
		frame.setVisible(true);
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                
        
	}

	public class MyKeyListener implements KeyListener { 
            
		@Override
		public void keyTyped(KeyEvent e) {
		}

		@Override
		public void keyPressed(KeyEvent e) {
                    keyPressed = KeyEvent.getKeyText(e.getKeyCode());
//			System.out.println("keyPressed="+KeyEvent.getKeyText(e.getKeyCode()));
//			System.out.println(keyPressed);
		}

		@Override
		public void keyReleased(KeyEvent e) {
//                    keyPressed = "";
//			System.out.println("keyReleased="+KeyEvent.getKeyText(e.getKeyCode()));
		}

      

       
        
	}
}
